package com.sx.art2.controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

import com.sx.art2.beans.User;
import com.sx.art2.service.UserService;


@Controller
@SessionAttributes(value={"list"},types={User.class})
public class UserController {
	
	@Autowired
	UserService userService;
	@RequestMapping("/userAdd")
	public ModelAndView addUser(User user) {
		
		ModelAndView mv=new ModelAndView();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式	
		user.setUserDate(df.format(new Date()));
		userService.add(user);
		mv.setViewName("login");
		return mv;
	}
	@RequestMapping("/userDelete")
	public ModelAndView deleteUser(int id) {
		
		ModelAndView mv=new ModelAndView();
		userService.delete(id);
		mv.setViewName("redirect:/userList");
		return mv;
	}
	@RequestMapping("userSelectById")
	public ModelAndView selectById(HttpSession session) {
		
		ModelAndView mv=new ModelAndView();
		User u=(User) session.getAttribute("User");
		User user=userService.selectById(u.getUserId());
		mv.addObject("User1", user);
		mv.setViewName("vip");
		return mv;
	}
	@RequestMapping("/userList")
	public ModelAndView listUser() {		
		ModelAndView mv=new ModelAndView();
		List<User> list =userService.list();
		System.out.println(list);
		mv.addObject("list", list);
		mv.setViewName("admin/user_list");
		return mv;				
	}
	@RequestMapping("/userDeleteAll")
	public ModelAndView deleteAllUser(int userIds[]) {		
		ModelAndView mv=new ModelAndView();
		userService.deleteAll(userIds);
		mv.setViewName("redirect:/userList");
		return mv;				
	}
	 @RequestMapping("/member")
	 public ModelAndView member(int userId){
		 ModelAndView mv=new ModelAndView();
		 List<User> listOne=userService.listOne(userId);
		 System.out.println(listOne);
		 mv.addObject("listOne",listOne);
		 mv.setViewName("admin/member-show");
		 return mv;		 
	 }
	 
	@RequestMapping("userUpdate")
	public ModelAndView updateUser(HttpSession session, User user) {
		
		ModelAndView mv=new ModelAndView();
		User u=(User) session.getAttribute("User");
		user.setUserId(u.getUserId()); 
		userService.update(user);
		mv.setViewName("redirect:/userSelectById");
		return mv;
	}	
	@RequestMapping("userSelectByPhone")
	public ModelAndView selectByPhone(@RequestParam("userPassword") String userPassword,String userPhone) {
		
		ModelAndView mv=new ModelAndView();
		User user=userService.selectByPhone(userPhone);
		if(user.getUserPassword().equals(userPassword)) {
			mv.addObject("User", user);
			mv.setViewName("index");
		}
		else {
			mv.addObject("error", "用户名或密码输入错误");
			mv.setViewName("login");
		}		
		return mv;
	}
	
	@RequestMapping("passwordUpdate")
	public ModelAndView passwordUpdate(HttpSession session, String userPassword,String userNewPassword) {
		
		ModelAndView mv=new ModelAndView();
		User user=(User) session.getAttribute("User");
		if(user.getUserPassword().equals(userPassword)) {
			user.setUserPassword(userNewPassword);
			userService.updatePassword(user);
			mv.addObject("User", user);
			mv.setViewName("login");
		}
		else {
			mv.addObject("message","原密码输入错误");
			mv.setViewName("password");
		}		
		return mv;
	}
	
	@RequestMapping("exist")
	@ResponseBody
public String exist(String userPhone) {
	
		User user=userService.selectByPhone(userPhone);
		String result=null;
		if(user==null) {
			result="notExist";			
		}else {
			result="exist";
		}	
		return result;
	}
	@RequestMapping("exit")
    public ModelAndView exit(HttpSession session,SessionStatus sessionStatus) {
		
		ModelAndView mv=new ModelAndView();		
        session.invalidate(); 
        sessionStatus.setComplete();
		mv.setViewName("login");
		return mv;
	}
	
}
